package com.lin.oss.adapter;

import com.lin.oss.entity.FileInfo;
import com.lin.oss.utils.MinioUtil;
import lombok.SneakyThrows;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

public class AliYunStorageAdapter implements StorageAdapter {
    @Resource
    private MinioUtil minioUtil;


    /**
     * 创建bucket桶
     *
     * @param butcket
     */
    @Override
    @SneakyThrows
    public void createBucket(String butcket) {
        minioUtil.createBucket(butcket);
    }

    /**
     * 上传文件
     *
     * @param multipartFile
     * @param bucket
     * @param objectName
     */
    @Override
    @SneakyThrows
    public void uploadFile(MultipartFile multipartFile, String bucket, String objectName) {
        minioUtil.createBucket(bucket);
        if (objectName.isBlank()) {
            minioUtil.uploadFile(multipartFile.getInputStream(), bucket, multipartFile.getName());
        } else {
            minioUtil.uploadFile(multipartFile.getInputStream(), bucket, objectName + '/' + multipartFile.getName());
        }
    }

    /**
     * 列出所有桶
     */
    @Override
    @SneakyThrows
    public List<String> getAllBucket() {
        System.out.println("阿里云");
        return minioUtil.getAllBucket();
    }

    /**
     * 列出当前桶的文件
     *
     * @param butcket
     */
    @Override
    @SneakyThrows
    public List<FileInfo> getAllFile(String butcket) {
        System.out.println("阿里云");
        return minioUtil.getAllFile(butcket);
    }

    /**
     * 下载文件
     *
     * @param butcket
     * @param objectName
     */
    @Override
    @SneakyThrows
    public InputStream downLoad(String butcket, String objectName) {
        return minioUtil.downLoad(butcket, objectName);
    }

    /**
     * 删除桶
     *
     * @param butcket
     */
    @Override
    @SneakyThrows
    public void deleteBucket(String butcket) {
        minioUtil.deleteBucket(butcket);
    }

    /**
     * 删除文件
     *
     * @param butcket
     * @param objectName
     */
    @Override
    @SneakyThrows
    public void deleteObject(String butcket, String objectName) {
        minioUtil.deleteObject(butcket, objectName);
    }

    @Override
    public String getUrl(String bucket, String objectName) {
        return "";
    }
}
